package com.kaifamiao.month03.day0325;

import java.util.Scanner;

public class Average {

    public static void main(String[] args) {
        int n = 5;
        int[] nums = input(n);
        System.out.println("您输入的 " + n + " 个数的平均值是：" + average(nums));
    }

    /**
     * 循环输入数组元素
     * @param n 数组元素个数
     * @return 输入的数组
     */
    public static int[] input(int n ){
        Scanner in = new Scanner(System.in);
        int[] nums = new int[n];
        for (int i = 0; i < nums.length; i ++){
            System.out.println("请输入第 " + (i + 1) +" 个数：");
            nums[i] = in.nextInt();
        }
        return nums;
    }

    /**
     * 计算平均值
     * @param nums 需要将计算平均值的数组
     * @return 计算的平均值
     */
    public static double average(int[] nums) {
        int sum = 0;
        for (int num : nums)
            sum += num;
        return (double) sum / (double) nums.length;
    }
}
